package com.guang.lock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lg
 * @Description
 * @date 2022/10/17 10:54
 */

public class ReentrantLockDemo2 {
    private static Logger logger = LoggerFactory.getLogger(ReentrantLockDemo2.class);
    static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                // 加锁成功之后也可以来进行操作
                if (Thread.currentThread().isInterrupted()){
                    // 补偿diamante
                    logger.info("当前的线程名称是：{}",Thread.currentThread().getName());
                }else {
                    logger.info("start operation");
                }
            }finally {
                lock.unlock();
                logger.info("当前线程的标记状态为：{}",Thread.currentThread().isInterrupted());
                // 擦除掉当前线程的标记状态，因为如果需要使用到线程池的时候，如果标记状态没有擦除的话，会对下一个线程造成影响
                Thread.interrupted();
            }
        }, "t1");
        t1.start();
        t1.interrupt();

    }
}
